const EventEmitter = require('events');

class PromiseWithEvents extends EventEmitter {
    constructor() {
        super();
        this.promise = null;
    }

    // 创建Promise并监听事件
    createPromise() {
        this.promise = new Promise((resolve, reject) => {
            this.on('success', resolve);
            this.on('error', reject);
        });
        return this.promise;
    }

    // 触发成功事件
    resolve(data) {
        this.emit('success', data);
    }

    // 触发失败事件
    reject(error) {
        this.emit('error', error);
    }
}

// 使用示例
const promiseWithEvents = new PromiseWithEvents();
const promise = promiseWithEvents.createPromise();

promise.then(data => {
    console.log('Promise resolved:', data);
}).catch(error => {
    console.log('Promise rejected:', error);
});

// 触发事件
promiseWithEvents.resolve('Hello World');